<?php
namespace app\admin\controller;

use think\App;
use think\facade\View;
use think\facade\Db;

use app\BaseController;
use app\admin\model\SysMenu as SysMenuModel;

class Base extends BaseController
{
    protected $uid;
    protected $username;

    public function __construct(App $app)
    {
        parent::__construct($app);

        //判断是否登陆
        if (!session('uid')) {
            $this->error('您还没有登录, 请登录', '/admin/login');
        }

        // 登录用户信息
        $this->uid = session('uid');
        $this->username = session('username');
        
        $rid = Db::name('sys_user')->where('id', $this->uid)->value('role_id');
        
        
        //菜单数据
        $sysMenuModel = new SysMenuModel();
        $data_menu = obj_tree($sysMenuModel->getUserMenuList($rid));
        View::assign('data_menu', $data_menu);

        // 当前控制器及方法(转小写)
        $controller = strtolower(preg_replace('/(?<=[a-z])([A-Z])/', '_$1', $this->app->request->controller()));
        $action = strtolower($this->app->request->action());
        
        // 当前活动菜单父id
        $menu_url = $controller.'/index';   
        $active_pid =  $sysMenuModel->where('url', $menu_url)->value('pid');
        View::assign('active_pid', $active_pid);

        //权限检查
        if (!$this->checkAuthor($rid, $controller, $action)) {
            $this->error('你无权限操作');
        }

        View::assign('controller', $controller);
        View::assign('action', $action);
        
        //记录日志
        $this->addLog();
    }

    /**
     * 权限检查
     */
    private function checkAuthor($rid, $c, $a)
    {
        if (!$rid) {
            return false;
        }

        if ($rid==1) {
            return true;
        }

        if (preg_match('/^public_/', $a)) {
            return true;
        }
        if ($c == 'index' && $a == 'index') {
            return true;
        }
        
        $sysMenuModel = new SysMenuModel();
        $menuList = $sysMenuModel->getUserMenuList($rid);

        foreach ($menuList as $v) {
            if ($v->url == $c.'/'.$a) {
                return true;
            }
        }
        return false;
    }

    /**
     * 记录日志
     */
    private function addLog()
    {
        $data = [];
        $data['querystring'] = $this->app->request->query()?'?'.$this->app->request->query():'';
        $data['c'] = $this->app->request->controller();
        $data['a'] = $this->app->request->action();
        $data['userid'] = $this->uid;
        $data['username'] = $this->username;
        $data['ip'] = $this->app->request->ip();
        $data['time'] = time();
        $arr = [];
        // $logLevel = 0;
        $logLevel = 1;
        // $logLevel = 2;
        switch ($logLevel) {
            case 2:
                $arr = array_merge($arr, ['SysUser/index','SysRole/index', 'SysMenu/index']);
            case 1:
                $arr = array_merge($arr, ['Category/index','Article/index', 'FileManager/index']);    
            default:
                $arr = array_merge($arr, ['Index/index','Index/usedspace','SysLog/index']);
                break;
        }
        if (!in_array($data['c'].'/'.$data['a'], $arr)) {
            Db::name('sys_log')->insert($data);
        }
    }

    /**
     * 删除
     */
    public function delete($id)
    {
        $model = "\\app\\admin\\model\\" . basename(get_called_class());
        
        if ($model::destroy($id)) {
            return ['code' => 1,'msg'=>"删除成功"];
        } else {
            return ['code' => 0,'msg'=>"删除失败"];
        }
    }
}
